<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Code by TinhNQ
 * 2014
 **/
class Region extends Admin_Controller {

    function __construct(){
        parent::__construct();
        $this->admin_navigation->parent_active('region');
        $this->load->library('session');
        $this->output->enable_profiler(TRUE);
        $this->load->model('pref/regions_model');
    }

    function manager_region() {
        $columns = array(
                array(
                    'name' => 'ID',
                    'type' => 'id',
                    'index' => 'region_id',
                    'filter' => 'region_id',
                    'sort_column' => true,
                ),
                array(
                    'name' => 'Khu vực',
                    'type' => 'text',
                    'index' => 'name',
                    'filter' => 'name',
                    'sort_column' => true,
                ),
                array(
                    'name' => 'Trạng thái',
                    'type' => 'publish',
                    'index' => 'status',
                    'filter' => 'status',
                    'sort_column' => true,
                    'options' => array('0' => 'Không', '1' => 'Có'),
                ),

            );
        $this->grid->columns($columns);
        $this->grid->datasource('pref/regions_model','get_regions');
        $this->grid->set_title('Region Manager');
        $this->grid->add_button('Thêm mới',admin_url('*/region/add_region'),'add');
        $this->grid->set_row_action('edit_region');
        $this->grid->total_rows($this->regions_model->get_count());
        $this->grid->add_mass_action('delete', array(
                'label'=>'Xóa',
                'url'      => admin_url('*/region/delete_region'),
                'confirm'  => 'Are you sure?'
        ));
        $this->grid->add_mass_action('status', array(
                'label'=>'Thay đổi trạng thái',
                'url'      => admin_url('*/region/change_status_region'),
        ));
        if ($this->input->is_ajax()){
            $this->output->set_output($this->grid->display());
        }else{
            $this->load->view('admin/grid');
        }
    }

    function edit_region() {
        $region_id = $this->input->get_param('id', false);

        if ($region_id) {
            $data = $this->regions_model->get_region($region_id);
            $this->form->set_values($data);
        }
        $this->form->set_form_attributes(array(
                'id' => 'region_form',
                'action' => admin_url('*/region/edit_post_region' , array('id' => $region_id)),
                'enctype' => 'multipart/form-data',
                ));
        $this->form->add_filedset('ganeral', array('legend' => 'General'));
        $this->form->add_filed('name', 'text', array('label' => 'Khu vực', 'name' => 'name', 'required' => true,));
        $this->form->add_filed('alias', 'text', array('label' => 'Alias', 'name' => 'alias', ));
        $this->form->add_filed('Trạng thái', 'select', array(
                'label' => 'Trạng thái',
                'name' => 'status',
                'values'=>array('Tắt','Bật')));

        $this->load->view('admin/form', array('form' => $this->form->to_html()));
    }

    function add_region() {
        $this->_forward('edit_region');
    }

    function delete_region() {
        $cids = explode(',', $this->input->get_param('region_id',false));
        foreach ($cids as $cid) {
            $this->regions_model->delete_region($cid);
        }
        $this->_redirect(admin_url('*/region/manager_region'));
    }

    function change_status_region() {
        $cids = explode(',', $this->input->get_param('region_id',false));
        foreach ($cids as $cid) {
            $this->regions_model->change_status_region($cid);
        }
        $this->_redirect(admin_url('*/region/manager_region'));
    }

    function edit_post_region() {
        $region_id = $this->input->get_param('id', false);
        $this->load->model('region/regions_model');
        if (($data = $this->input->post())) {

            if ($region_id) {
                $this->regions_model->update_region($data, $region_id);
            } else {
                $this->regions_model->insert_region($data);
            }
            $this->_redirect(admin_url('*/region/manager_region'));
        }
    }
}
?>